//
//  Judgment.java
//  ics314-calendar
//
//  Created by ICS314 Team Mississippi  on 7/30/14.
//  Copyright (c) 2014 JinHao Chen, Christian Caluya,Kevin Fujikawa. All rights reserved.
//
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.TimeZone;

import javax.swing.JCheckBox;

public class Judgment {
	/**
	 * Returns an boolean of the the user input date is validation format or not
	 *
	 * @param  s  the user input of the Date string
	 * @return  true for the date is correct format 
	 * <p>		
	 * false for the date is incorrect format
	 * @throws  ParseException  when the date is invalidation
	 */
	public static boolean isDate(String s){
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		Date d = null;  
        try {
			d = dateFormat.parse(s);
	        String temp = dateFormat.format(d);
	        if (s.equals(temp)) {
				return true;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * Returns an integer to represent the comparison result between two date.
	 * @param  date1  the first date
	 * @param  date2  the second date
	 * @return  positive for date > date, negative for date1<date2, and zero for date1 == date2
	 * @throws  ParseException  when the date1 or date2  is invalidation
	 */
	public static int compareDate(String date1, String date2){
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		 
        try {
			Date d = dateFormat.parse(date1);
			Date d2 = dateFormat.parse(date2);
			return d.compareTo(d2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return 0;
	}
	
	/**
	 * Returns the java.util.date format to RFC5545 date format
	 * @param  date  the date
	 * @param  time  the time
	 * @return  the RFC5545 date format of the date and time
	 */
	public static String convertDateAndTime(String date,String time){
		String[] s = date.split("/");
		String[] s2 = time.split(":");
		return s[2]+s[0]+s[1]+"T"+s2[0]+s2[1]+s2[2];
	}
	
	/**
	 * Returns a string of time zone offset that suitable in .ics file when at standard time
	 * @param  timeZone the java.util.TimeZone
	 * @return  a string of time zone offset
	 */
	public static String convertTZOffSetTo(String timeZone){
		TimeZone tz  = TimeZone.getTimeZone(timeZone);
		int a = tz.getRawOffset()/(3600*1000);
		if (a>=10){
			return "+"+a+"00";
		} 
		if (a<=-10){
			return a+"00";
		}
		if (a>=0 && a<10){
			return "+0"+a+"00";
		}
		if (a<0 && a>-10){
			return"-0"+Math.abs(a)+"00";
		}
		return "";
	}
	/**
	 * Returns the java.util.date format to RFC5545 date format when at daylight saving
	 * @param  date  the date
	 * @param  time  the time
	 * @return  the RFC5545 date format of the date and time
	 */
	public static String convertTZOffSetFrom(String timeZone){
		TimeZone tz  = TimeZone.getTimeZone(timeZone);
		int a = tz.getRawOffset()/(3600*1000);
		if (!tz.useDaylightTime()) return convertTZOffSetTo(timeZone);
		if (a>=10){
			return "+"+(a+1)+"00";
		} 
		if (a<=-10){
			if (a==-10) return "-0900";
			return (a+1)+"00";
		}
		if (a>=0 && a<10){
			if (a==10) return "+1100";
			return "+0"+(a+1)+"00";
		}
		if (a<0 && a>-10){
			if (a==-1) return "+0000";
			return"-0"+Math.abs(a+1)+"00";
		}
		return "";
	}
	
	/**
	 * Returns a string of "DAILY", "WEEKLY", "MONTHLY" or "YEARLY"
	 * @param  repeatSelector  the selector of recurring event.
	 * @return  a string of "DAILY", "WEEKLY", "MONTHLY" or "YEARLY"
	 */
	public static String getRepeatFREQ(String repeatSelector){
		if (repeatSelector.equals( "None" )) return "";
		if (repeatSelector.equals("Every Day")) return "DAILY";
		if (repeatSelector.equals("Every Week")) return "WEEKLY";
		if (repeatSelector.equals("Every Month")) return "MONTHLY";
		if (repeatSelector.equals("Every Year")) return "YEARLY";
		return "";
	}
	
	/**
	 * Returns a string "MO","TU","WE","TH","FR","SA", or "SU" (Can more than one)
	 * @param  checkBox[]  the checkBox for the recurring event select for Weekly
	 * @return  {"MO","TU","WE","TH","FR","SA","SU"} some of those
	 */
	public static String convertRepeatByWeek(JCheckBox checkBox[]){
		String[] AllDay = new String[]{"MO","TU","WE","TH","FR","SA","SU"};
		String result = "BYDAY=";
		for (int i = 0 ; i<checkBox.length ; i++ ){
			if (checkBox[i].isSelected()){
				result += AllDay[i]+",";
			}
		}
		if (!result.equals("BYDAY=")) result = result.substring(0, result.length()-1);
		return result;
	}
	/**
	 * Returns a string {0, 1, 2, 3..., 10 , 11} (Can more than one)
	 * @param  checkBox[]  the checkBox for the recurring event select for Yearly
	 * @return  {0, 1, 2, 3..., 10 , 11} some of those
	 */
	public static String convertRepeatByYear(JCheckBox checkBox[]){
		String result = "BYMONTH=";
		for (int i = 0 ; i<checkBox.length ; i++ ){
			if (checkBox[i].isSelected()){
				result += (i+1)+",";
			}
		}
		if (!result.equals("BYMONTH=")) result = result.substring(0, result.length()-1);
		return result;
	}
}
